package me.shurufa.holder;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.shurufa.R;
import me.shurufa.model.Comment;
import me.shurufa.utils.Utils;

/**
 * 用户评价列表的ViewHolder
 *
 * @FileName: me.shurufa.holder.UserCommentViewHolder.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-28 17:00
 */
public class UserCommentViewHolder extends BaseViewHolder{

    @Bind(R.id.book_img)
    ImageView bookImg;
    @Bind(R.id.book_title)
    TextView bookTitle;
    @Bind(R.id.description_view)
    TextView descriptionView;
    @Bind(R.id.comment_date)
    TextView commentDate;
    @Bind(R.id.expand_view)
    ImageView expandView;
    @Bind(R.id.description_layout)
    LinearLayout descriptionLayout;

    //最多显示5行
    int maxDescripLine = 2;
    //是否展开
    boolean isExpand;

    private Context mContext;
    private Comment mComment;

    public UserCommentViewHolder(View itemView, Object parent) {
        super(itemView, parent);
        mContext = itemView.getContext();
        ButterKnife.bind(this, itemView);
        addOnClickListener(expandView);
    }

    public void bind(Comment comment){
        if (comment == null) {
            return;
        }
        mComment = comment;
        if (comment.book_info!=null){
            if(!TextUtils.isEmpty(comment.book_info.image)){
                Glide.with(mContext).load(comment.book_info.image).placeholder(R.drawable.ic_book_loading).error(R.drawable.ic_book_loading).into(bookImg);
            }else {
                bookImg.setImageResource(R.drawable.ic_book_loading);
            }
            if (!TextUtils.isEmpty(comment.book_info.title)){
                bookTitle.setText(comment.book_info.title);
            }
        }

        if (!TextUtils.isEmpty(comment.content)){
            descriptionView.setText(comment.content);
            descriptionView.setHeight(descriptionView.getLineHeight() * maxDescripLine);
            descriptionView.post(new Runnable() {
                @Override
                public void run() {
                    expandView.setVisibility(descriptionView.getLineCount() > maxDescripLine ? View.VISIBLE : View.GONE);
                }
            });

        }
        if (comment.dateline!=0L){
            commentDate.setText(Utils.getDatetimeStr(comment.dateline*1000L));
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.expand_view:
            {
                isExpand = !isExpand;
                descriptionView.clearAnimation();
                final int deltaValue;
                final int startValue = descriptionView.getHeight();
                int durationMillis = 350;
                if (isExpand) {
                    deltaValue = descriptionView.getLineHeight() * descriptionView.getLineCount() - startValue;
                    RotateAnimation animation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    animation.setDuration(durationMillis);
                    animation.setFillAfter(true);
                    expandView.startAnimation(animation);
                } else {
                    deltaValue = descriptionView.getLineHeight() * maxDescripLine - startValue;
                    RotateAnimation animation = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    animation.setDuration(durationMillis);
                    animation.setFillAfter(true);
                    expandView.startAnimation(animation);
                }
                Animation animation = new Animation() {
                    protected void applyTransformation(float interpolatedTime, Transformation t) {
                        descriptionView.setHeight((int) (startValue + deltaValue * interpolatedTime));
                    }

                };
                animation.setDuration(durationMillis);
                descriptionView.startAnimation(animation);
            }
                break;
        }
    }
}
